package screens;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public abstract class AbstractVendasCanvas extends Canvas {

	protected int coluna1 = 0;
	protected int coluna2 = 0;
	protected int coluna3 = 0;
	protected String coluna1Label = "";
	protected String coluna2Label = "";
	protected String coluna3Label = "";

	int x = 10;
	int y = 10;
	int codigo;
	String evento = "";

	protected void paint(Graphics g) {
		// Limpa a tela
		g.setColor(255, 255, 255);
		g.fillRect(0, 0, getWidth(), getHeight());

		// Desenha o cursor na tela
		g.setColor(0, 0, 0);
		g.drawLine((x - 5), y, (x + 5), y);
		g.drawLine(x, (y - 5), x, (y + 5));

		// Valida os limites de X e Y
		if (x < 0)
			x = 0;
		if (y < 0)
			y = 0;
		if (x > getWidth())
			x = getWidth();
		if (y > getHeight())
			y = getHeight();

		// Desenha o grafico
		// Eixo X
		g.drawLine((int) (getWidth() * 0.1), (int) (getHeight() * 0.9),
				(int) (getWidth() * 0.9), (int) (getHeight() * 0.9));
		// Eixo Y
		g.drawLine((int) (getWidth() * 0.1), (int) (getHeight() * 0.3),
				(int) (getWidth() * 0.1), (int) (getHeight() * 0.9));
		// Barras
		g.setColor(255, 0, 0);
		double i = (getHeight() * 0.6)
				- ((coluna1 * (getHeight() * 0.6)) / 100);
		g.fillRect((int) (getWidth() * 0.1) + 5, (int) (getHeight() * 0.3)
				+ (int) i, (int) (getWidth() * 0.1), (int) (getHeight() * 0.6)
				- (int) i);

		g.setColor(0, 255, 0);
		i = (getHeight() * 0.6) - ((coluna2 * (getHeight() * 0.6)) / 100);
		g.fillRect((int) (getWidth() * 0.3) + 5, (int) (getHeight() * 0.3)
				+ (int) i, (int) (getWidth() * 0.1), (int) (getHeight() * 0.6)
				- (int) i);

		g.setColor(0, 0, 255);
		i = (getHeight() * 0.6) - ((coluna3 * (getHeight() * 0.6)) / 100);
		g.fillRect((int) (getWidth() * 0.5) + 5, (int) (getHeight() * 0.3)
				+ (int) i, (int) (getWidth() * 0.1), (int) (getHeight() * 0.6)
				- (int) i);

		// Escreve a Legenda na tela
		if (coluna1 != 0) {
			g.setColor(255, 0, 0);
			g.fillRect((int) (getWidth() * 0.15), (int) (getHeight() * 0.1),
					10, 10);
			g.setColor(0, 0, 0);
			g.drawString(coluna1Label + ": " + coluna1,
					(int) (getWidth() * 0.15) + 10, (int) (getHeight() * 0.1),
					Graphics.TOP | Graphics.LEFT);
		}
		if (coluna2 !=0) {
			g.setColor(0, 255, 0);
			g.fillRect((int) (int) (getWidth() * 0.15),
					(int) (getHeight() * 0.1) + 15, 10, 10);
			g.setColor(0, 0, 0);
			g.drawString(coluna2Label + ": " + coluna2,
					(int) (getWidth() * 0.15) + 10,
					(int) (getHeight() * 0.1) + 15, Graphics.TOP
							| Graphics.LEFT);
		}

		if (coluna3!=0) {
			g.setColor(0, 0, 255);
			g.fillRect((int) (int) (getWidth() * 0.15),
					(int) (getHeight() * 0.1) + 30, 10, 10);
			g.setColor(0, 0, 0);
			g.drawString(coluna3Label + ": " + coluna3,
					(int) (getWidth() * 0.15) + 10,
					(int) (getHeight() * 0.1) + 30, Graphics.TOP
							| Graphics.LEFT);
		}
	}

	protected void keyPressed(int keyCode) {
		int action = getGameAction(keyCode);
		if (action == Canvas.UP) {
			y--;
		} else if (action == Canvas.DOWN) {
			y++;
		} else if (action == Canvas.LEFT) {
			x--;
		} else if (action == Canvas.RIGHT) {
			x++;
		}
		codigo = keyCode;
		evento = "keyPressed";
		repaint();
	}

	protected void keyReleased(int keyCode) {
		codigo = keyCode;
		evento = "keyReleased";
		repaint();
	}

	protected void keyRepeated(int keyCode) {
		int action = getGameAction(keyCode);
		if (action == Canvas.UP) {
			y--;
		} else if (action == Canvas.DOWN) {
			y++;
		} else if (action == Canvas.LEFT) {
			x--;
		} else if (action == Canvas.RIGHT) {
			x++;
		}
		codigo = keyCode;
		evento = "keyRepeated";
		repaint();
	}

	public void setColuna1(int coluna1) {
		this.coluna1 = coluna1;
	}

	public void setColuna2(int coluna2) {
		this.coluna2 = coluna2;
	}

	public void setColuna3(int coluna3) {
		this.coluna3 = coluna3;
	}

	public void setColuna1Label(String coluna1Label) {
		this.coluna1Label = coluna1Label;
	}

	public void setColuna2Label(String coluna2Label) {
		this.coluna2Label = coluna2Label;
	}

	public void setColuna3Label(String coluna3Label) {
		this.coluna3Label = coluna3Label;
	}

}
